const app = getApp()
Page({
  data: {
    globalData: app.globalData,
    userInfo: {},
    check: '0',
    isClose: false,
    phoneNumber: ''
  },
  onLoad: function() {
    var that = this;
    // app.userLogin();
    //获取用户数据缓存
    wx.getStorage({
      key: 'users',
      success: function(e) {
        console.log(e);
        app.globalData.userInfo = e.data;
        that.setData({
          userInfo: e.data
        });
      }
    })
  },
  onLaunch: function() {

  },
  //test20181106 aa 13524684535
  //事件处理函数
  checkboxChange: function(e) {
    //console.log('checkbox发生change事件，携带value值为：', e.detail.value)
    if (e.detail.value == '1') {
      this.setData({
        check: '1'
      })
      console.log('同意服务声明')
    } else {
      this.setData({
        check: '0'
      })
      console.log('未同意服务声明')
    }
  },
  eMsg: function(e) {
    //console.log('111')
    wx.showToast({
      title: '同意服务声明才能登录',
      icon: 'none',
      duration: 2000
    })
  },
  //点击关闭详情
  clickClose: function(e) {
    this.setData({
      isClose: false
    });
  },
  ServerTxt: function(e) {
    this.setData({
      isClose: true
    })
  },
  formSubmit: function(event) {
    console.log('formSubmit---')
    console.log('form发生了submit事件，携带数据为：', event.detail.value)
    var userInfo = event.detail.value;

    if (userInfo.phone.length != 11) {
      wx.showToast({
        title: '手机号长度有误！',
        icon: 'none',
        mask: true,
        duration: 2000
      })
      return false;
    }

    var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    if (!myreg.test(userInfo.phone)) {
      wx.showToast({
        title: '手机号有误！',
        icon: 'none',
        mask: true,
        duration: 2000
      })
      return false;
    }

    if (userInfo.storeId == '' || userInfo.name == '' || userInfo.name == '') {
      wx.showToast({
        title: '请输入信息',
        icon: 'none',
        mask: true,
        duration: 2000
      })
    } else {
      var that = this;
      userInfo.openId = app.openId;
      // 校验
      app.request({
        url: "/wxMatching",
        data: userInfo,
        success: function(e) {
          app.globalData.userInfo = userInfo;
          that.setData({
            globalData: app.globalData
          })
          wx.hideLoading()
          wx.setStorage({
            key: 'users',
            data: userInfo,
            success: function(e) {
              console.log('登录成功', e)
              wx.navigateTo({
                url: '../index/index'
              })
            }
          })
        }
      })
    }
  },
  //获取当前绑定的手机号码
  getPhoneNumber: function(e) {
    if (typeof(e.detail.iv) == "undefined") {
      var that = this;
      wx.showModal({
        title: '提示',
        showCancel: false,
        content: '未授权',
        mask: true,
        success: function(res) {
          console.log('点击了取消')
          that.setData({
            phoneNumber: ''
          })
        }
      })
    } else {
      var that = this;
      wx.showModal({
        title: '提示',
        showCancel: false,
        content: '授权成功',
        mask: true,
        success: function(res) {
          wx.login({
            success: res => {
              app.request({
                url: "/code2OpenId",
                data: {
                  code: res.code,
                  iv: e.detail.iv,
                  encryptedData: encodeURIComponent(e.detail.encryptedData)
                },
                success: function(w) {
                  console.log('点击了同意', w)
                  app.globalData.userInfo.phone = w.data.data.phoneNumber;
                  app.openId = w.data.data.openId;
                  that.setData({
                    globalData: app.globalData
                  })
                  console.log(app.globalData.userInfo.phone);
                  //console.log(that.data.userInfo.phone);
                }
              })
            }
          })
        }
      })
    }
  },
  login: function(data) {
    console.log('login-------', data)
    
  }
})